package com.wanmait.demo.webcontroller.usercontroller;

import com.wanmait.demo.pojo.*;
import com.wanmait.demo.service.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class RepairController {
    @Resource
    private DistrictService districtService;
@Resource
private TimetableService timetableService;
@Resource
private RepairOrderService repairOrderService;
@Resource
private AddressService addressService;
@Resource
private OrderBackService orderBackService;
@Resource
    private WorkerService workerService;
    @RequestMapping("/repair")
    public ModelAndView repair(){
        ModelAndView modelAndView=new ModelAndView();
        List<District> districts=districtService.selectalldistrict();
        List<Timetable> timetables=timetableService.selectalltime();

        modelAndView.addObject("districts",districts);
        modelAndView.addObject("timetables",timetables);
        modelAndView.setViewName("user/ysk/repair");
        return modelAndView;

    }


    @RequestMapping("/submitone")
    public void submitone(String name, String telnum, String des, HttpSession  session){
    Userinfo  userinfo=(Userinfo)session.getAttribute("loginUser");
    int userid=userinfo.getId();
    repairOrderService.insertinfo(userid,name,telnum,des);

}

    @RequestMapping("/picture")  //尹山坤
    public  ModelAndView picture(MultipartFile pic, HttpServletRequest request,String timetable,String username,String phonenum,String describe,String address) {

        Userinfo userinfo=(Userinfo)request.getSession().getAttribute("loginUser");
        if (!pic.isEmpty()) {
            String originalFileName = pic.getOriginalFilename();

            String ext = FilenameUtils.getExtension(originalFileName);

            if (!",jpg,jpeg,bmp,gif,png,".contains("," + ext + ",")) {

            }

            String fileName = UUID.randomUUID() + "." + ext;

            String realPath = request.getServletContext().getRealPath("/ysk/images");

            File file = new File(realPath, fileName);
            try {
                pic.transferTo(file);

            } catch (IOException e) {
                e.printStackTrace();
            }


            repairOrderService.insertrepairorder(fileName,username,phonenum,describe,Integer.parseInt(address),Integer.parseInt(timetable),userinfo.getId());

        }
        else {
            repairOrderService.insertrepairorder("",username,phonenum,describe,Integer.parseInt(address),Integer.parseInt(timetable),userinfo.getId());
        }
        addressService.updateuseraddressid(Integer.parseInt(address));//使用的设置为2

        //System.out.println(userinfo.getId());
        addressService.deleteotheraddressid(userinfo.getId());//删除其他的

        //System.out.println("添加ok");
         //订单进度
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("forward:/user/allrepairorder");
        return modelAndView;
    }


//尹山坤
@RequestMapping("/ceshipic")
    public String ceshipic(){
       return "user/ysk/ceshipic" ;
}


    @RequestMapping("/allrepairorder")
    public ModelAndView allrepairorder(HttpSession session){
        Userinfo userinfo=(Userinfo)session.getAttribute("loginUser");
        ModelAndView modelAndView=new ModelAndView();
        List<RepairOrder> orders=repairOrderService.selectnotreceivingorders(userinfo.getId());

        modelAndView.addObject("orders",orders);
        modelAndView.setViewName("user/ysk/underway");

        return modelAndView;
    }


    @RequestMapping("/adddizhi")
    public void adddizhi(String com,String address,HttpSession session){
        Userinfo userinfo=(Userinfo)session.getAttribute("loginUser");
        addressService.insertorderaddress(userinfo.getId(),1,Integer.parseInt(com),address);
        //System.out.println("添加地址成功");

    }

@ResponseBody
    @RequestMapping("/getmaxaddressid")
    public Integer getmaxaddressid(HttpSession session){
        Userinfo userinfo=(Userinfo)session.getAttribute("loginUser");
        Integer maxid=addressService.selectmaxaddressid(userinfo.getId());
    System.out.println(maxid);
        return maxid;
    }


    @RequestMapping("/ing")
    public String ing(){
        return "user/ysk/ing";
    }

@ResponseBody
    @RequestMapping("/getorderaddress")
    public String getorderaddress(String orderid){
       RepairOrder repairOrder= repairOrderService.selectorderaddress(Integer.parseInt(orderid));
       String address=repairOrder.getAdressId().getCompleteAddress();

       return address;
    }


 @ResponseBody
 @RequestMapping("/getordertime")
    public String getordertime(String timeid){
       Timetable timetable= timetableService.selecttime(Integer.parseInt(timeid));
       String time=timetable.getShijianduan();
        return time;
 }


 @RequestMapping("/refund")
    public void refund(String result,String orderid){
     //System.out.println(result);
       orderBackService.insertuserorder(Integer.parseInt(orderid),result);
       repairOrderService.updateorderstate(Integer.parseInt(orderid));

 }




    @RequestMapping("/ingorder")
    public ModelAndView ingorder(HttpSession session){
        Userinfo userinfo=(Userinfo)session.getAttribute("loginUser");
        ModelAndView modelAndView=new ModelAndView();
        List<RepairOrder> orders=repairOrderService.selectingorders(userinfo.getId());

        modelAndView.addObject("orders",orders);
        modelAndView.setViewName("user/ysk/ing");

        return modelAndView;
    }

@ResponseBody
    @RequestMapping("/getworker")
    public Worker getworker(String workerid){
        Worker worker=workerService.selectworker(Integer.parseInt(workerid));

        return worker;

}


}
